﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using Metro.Core;

namespace Weather
{
    /// <summary>
    /// Interaction logic for Options.xaml
    /// </summary>
    public partial class Options : Window
    {
        private List<CityLocation> _results = new List<CityLocation>();

        public Options()
        {
            InitializeComponent();
        }

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            //IconImage.Source = new BitmapImage(new Uri(E.Path + "\\Mail\\Resources\\icon.png"));

            SettingsTitle.Text = Widget.Instance.LocaleManager.GetString("Settings");
            GeneralPageHeader.Text = Widget.Instance.LocaleManager.GetString("General");
            AboutPageHeader.Text = Widget.Instance.LocaleManager.GetString("About");
            LocationTitle.Text = Widget.Instance.LocaleManager.GetString("Location");
            TemperatureScaleTitle.Text = Widget.Instance.LocaleManager.GetString("TemperatureScale");

            WidgetName.Text = Widget.Instance.LocaleManager.GetString("WidgetName");
            WidgetDescription.Text = Widget.Instance.LocaleManager.GetString("WidgetDescription");
            WidgetVersion.Text = Widget.Instance.LocaleManager.GetString("WidgetVersion") + " " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
            OkButton.Content = Widget.Instance.LocaleManager.GetString("OK");
            CancelButton.Content = Widget.Instance.LocaleManager.GetString("Cancel");
            ApplyButton.Content = Widget.Instance.LocaleManager.GetString("Apply");

            LocationBox.Text = ((WidgetControl) Widget.Instance.WidgetControl).WeatherReport.Location;
        }

        private void OkButtonClick(object sender, RoutedEventArgs e)
        {
            if (ApplyButton.IsEnabled)
                ApplySettings();
            this.Close();
        }

        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ApplyButtonClick(object sender, RoutedEventArgs e)
        {
            ApplySettings();
            ApplyButton.IsEnabled = false;
        }

        private void GeneralPageHeaderMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var s = (Storyboard)Resources["SwapAnimLeft"];
            s.Begin();
            GeneralPage.Visibility = Visibility.Visible;
            GeneralPageHeader.Foreground = Brushes.White;
            AboutPageHeader.Foreground = Brushes.Gray;
        }

        private void AboutPageHeaderMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var s = (Storyboard)Resources["SwapAnimRight"];
            s.Begin();
            AboutPage.Visibility = Visibility.Visible;
            GeneralPageHeader.Foreground = Brushes.Gray;
            AboutPageHeader.Foreground = Brushes.White;
        }

        private void SwapAnimRightCompleted(object sender, EventArgs e)
        {
            GeneralPage.Visibility = Visibility.Collapsed;
        }

        private void SwapAnimLeftCompleted(object sender, EventArgs e)
        {
            AboutPage.Visibility = Visibility.Collapsed;
        }

        private void SettingsChanged(object sender, RoutedEventArgs e)
        {
            ApplyButton.IsEnabled = true;
        }

        private void LocationBoxKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && !string.IsNullOrEmpty(LocationBox.Text))
            {
                string s = LocationBox.Text;
                ThreadStart threadStarter = delegate
                {
                    GetLocation(s);
                };
                Thread thread = new Thread(threadStarter);
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
            }
        }

        private void GetLocation(string l)
        {
            _results.Clear();

            /*SearchProgress.Dispatcher.Invoke((Action)delegate
            {
                SearchProgress.Visibility = System.Windows.Visibility.Visible;
            }, null);*/

            SearchResults.Dispatcher.Invoke((Action)delegate
            {
                SearchResults.Items.Clear();
            }, null);

            List<CityLocation> locations = ((WidgetControl)Widget.Instance.WidgetControl).CurrentProvider.GetLocation(l);
            if (locations != null && locations.Count > 0)
            {
                foreach (CityLocation location in locations)
                {
                    SearchResults.Dispatcher.Invoke((Action)delegate
                    {
                        ListBoxItem item = new ListBoxItem();
                        item.Content = location.City;
                        SearchResults.Items.Add(item);
                    }, null);
                    _results.Add(location);
                }
            }
            else
            {
                SearchResults.Dispatcher.Invoke((Action)delegate
                {
                    /*LocationItem item = new LocationItem();
                    item.Header = Widget.LocaleManager.GetString("NoResults");
                    item.IsEnabled = false;
                    SearchResults.Children.Add(item);*/
                }, null);
            }

            /*SearchProgress.Dispatcher.Invoke((Action)delegate
            {
                SearchProgress.Visibility = System.Windows.Visibility.Hidden;
            }, null);*/
        }

        private void ApplySettings()
        {
            Widget.Instance.Sett.DegreeType = (bool)Fahrenheit.IsChecked ? 1 : 0;

            Widget.Instance.UpdateSettings();
        }

        private void SearchResultsSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Widget.Instance.Sett.LocationCode = _results[SearchResults.SelectedIndex].Code;
             ((WidgetControl)Widget.Instance.WidgetControl).RefreshWeather();
        }
    }
}
